home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / attribs / cfgitemi.cpp < prev    next >
C/C++ Source or Header  |  2001-10-08  |  6KB  |  209 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29.  
  30. #include "cfgitemi.h"
  31. #include "attrcb.h"
  32. #include "attribute.h"
  33.  
  34. #include "../common/std.h"
  35. #include "../studio/api.h"
  36.  
  37. CfgItemI::CfgItemI(const char *name)
  38.   : Named(name)
  39. { }
  40.  
  41. CfgItemI::~CfgItemI() {
  42.   deregisterAll();
  43. }
  44.  
  45. const char *CfgItemI::cfgitem_getName() {
  46.   return Named::getName();
  47. }
  48.  
  49. int CfgItemI::cfgitem_getNumAttributes() {
  50.   return attributes.getNumItems();
  51. }
  52.  
  53. const char *CfgItemI::cfgitem_enumAttribute(int n) {
  54.   Attribute *attr = attributes[n];
  55.   if (attr) return attr->getAttributeName();
  56.   return NULL;
  57. }
  58.  
  59. int CfgItemI::cfgitem_getNumChildren() {
  60.   return children.getNumItems();
  61. }
  62.  
  63. CfgItem *CfgItemI::cfgitem_enumChild(int n) {
  64.   return children[n];
  65. }
  66.  
  67. Attribute *CfgItemI::getAttributeByName(const char *name) {
  68.   Attribute *attr;
  69.   for (int i = 0; i < attributes.getNumItems(); i++) {
  70.     attr = attributes[i];
  71.     if (!STRICMP(name, attr->getAttributeName())) return attr;
  72.   }
  73.   return NULL;
  74. }
  75.  
  76. int CfgItemI::getAttributeType(const char *name) {
  77.   Attribute *attr = getAttributeByName(name);
  78.   if (attr == NULL) return AttributeType::NONE;
  79.   return attr->getAttributeType();
  80. }
  81.  
  82. int CfgItemI::getDataLen(const char *name) {
  83.   Attribute *attr = getAttributeByName(name);
  84.   if (attr == NULL) return -1;
  85.   return attr->getDataLen();
  86. }
  87.  
  88. int CfgItemI::getData(const char *name, void *data, int data_len) {
  89.   Attribute *attr = getAttributeByName(name);
  90.   if (attr == NULL) return -1;
  91.   return attr->getData(data, data_len);
  92. }
  93.  
  94. int CfgItemI::setData(const char *name, void *data, int data_len) {
  95.   Attribute *attr = getAttributeByName(name);
  96.   if (attr == NULL) return -1;
  97.   return attr->setData(data, data_len);
  98. }
  99.  
  100. int CfgItemI::cfgitem_onAttribSetValue(Attribute *attr) {
  101.   // notify dependency watchers that an attribute changed
  102.   dependent_changedAttr(attr->getAttributeName());
  103.  
  104.   for (int i = 0; ; i++) {
  105.     AttrCallback *acb;
  106.     if (!callbacks.multiGetItem(attr, i, &acb)) break;
  107.     acb->onValueChange(attr);
  108.   }
  109.   
  110.   return 0;
  111. }
  112.  
  113. int CfgItemI::setName(const char *name) {
  114.   Named::setName(name);
  115. //FUCKO: notify dependency watchers that name changed?
  116.   return 1;
  117. }
  118.  
  119. int CfgItemI::registerAttribute(Attribute *attr, AttrCallback *acb) {
  120.   if (attributes.haveItem(attr)) return 0;
  121.   int ret = attributes.addItem(attr) != NULL;
  122.   if (!ret) return ret;
  123.   attr->setCfgItem(this);
  124.  
  125.   // set optional callback
  126.   if (acb != NULL)
  127.     addCallback(attr, acb);
  128.  
  129.   // notify derived classes too
  130.   cfgitem_onRegisterAttribute(attr);
  131.  
  132.   // notify dependency watchers of new attribute
  133.   dependent_addedAttr(attr->getAttributeName());
  134.  
  135.   return ret;
  136. }
  137.  
  138. int CfgItemI::deregisterAttribute(Attribute *attr) {
  139.   if (!attributes.haveItem(attr)) return 0;
  140.   int ret = attributes.removeItem(attr);
  141.   // notify dependency watchers of attribute going away
  142. //  dependent_deletedAttr(attr->getAttributeName());
  143.   // tell attribute
  144.   attr->setCfgItem(NULL);
  145.   // notify derived classes too, should be safe if they want to delete here
  146.   cfgitem_onDeregisterAttribute(attr);
  147.  
  148.   // remove callbacks
  149.   callbacks.multiDelAllForItem(attr, TRUE);
  150.  
  151.   return ret;
  152. }
  153.  
  154. void CfgItemI::addCallback(Attribute *attr, AttrCallback *acb) {
  155.   ASSERT(attr != NULL);
  156.   ASSERT(acb != NULL);
  157.   callbacks.multiAddItem(attr, acb);
  158. }
  159.  
  160. void CfgItemI::cfgitem_onRegisterAttribute(Attribute *attr) {
  161.   switch (attr->getAttributeType()) {
  162.     case AttributeType::BOOL: 
  163.     case AttributeType::INT: {
  164.       int v = _int_getValue(attr);    // current val
  165.       int ov = api->getIntPrivate(attr->getAttributeName(), v); // disk val
  166.       // force setting of it in case they have a callback or dependency viewer
  167.       _int_setValue(attr, ov);
  168.     }
  169.     break;
  170.   }
  171. }
  172.  
  173. void CfgItemI::cfgitem_onDeregisterAttribute(Attribute *attr) {
  174.   switch (attr->getAttributeType()) {
  175.     case AttributeType::BOOL: 
  176.     case AttributeType::INT: {
  177.       api->setIntPrivate(attr->getAttributeName(), _int_getValue(attr));
  178.     }
  179.     break;
  180.   }
  181. //  delete attr;//FUCKO: how do we know which to delete?
  182. }
  183.  
  184. void CfgItemI::deregisterAll() {
  185.   while (attributes.getNumItems()) deregisterAttribute(attributes[0]);
  186. }
  187.  
  188. void CfgItemI::addChildItem(CfgItemI *child) {
  189.   if (!children.haveItem(child))
  190.     children.addItem(child);
  191. }
  192.  
  193. void CfgItemI::cfgitem_shutdown() {
  194.   deregisterAll();
  195. }
  196.  
  197. #define CBCLASS CfgItemI
  198. START_DISPATCH
  199.   CB(GETNAME, cfgitem_getName);
  200.   CB(GETNUMATTRIBUTES, cfgitem_getNumAttributes);
  201.   CB(ENUMATTRIBUTE, cfgitem_enumAttribute);
  202. //CUT  CB(GETATTRIBUTEBYNAME, cfgitem_getAttributeByName);
  203.   CB(GETDEPENDENCYPTR, cfgitem_getDependencyPtr);
  204.   CB(GETNUMCHILDREN, cfgitem_getNumChildren);
  205.   CB(ENUMCHILD, cfgitem_enumChild);
  206.   VCB(SHUTDOWN, cfgitem_shutdown);
  207. END_DISPATCH
  208. #undef CBCLASS
  209.